﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        string cmdString = "select * from Books";

        private void MainForm_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = DbHelper.GetTable(cmdString);
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToResizeColumns = false;
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;//自动设置列宽
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var search =string.Format("select * from Books where book_name like '%{0}%' or author like '%{0}%'",name);
            var dt = DbHelper.GetTable(search);
            dataGridView1.DataSource = dt;

        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            EditForm editform = new EditForm();
            editform.Show();
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = DbHelper.GetTable(cmdString);
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToResizeColumns = false;
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];

                var id = row.Cells[0].Value;

                var res = MessageBox.Show("是否确定删除", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (res.Equals(DialogResult.Yes))
                {
                    var dele = string.Format("delete from Books where id = {0}", id);
                    var rowCount = DbHelper.AddOrEditOrDeleSave(dele);
                    if (rowCount > 0)
                    {
                        dataGridView1.DataSource = DbHelper.GetTable(cmdString);
                        MessageBox.Show("删除成功", "提示");
                    }
                }
            }
            else
            {
                MessageBox.Show("未选择要删除的行", "提示");
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {

        }
    }
}
